package com.sk.weichat.db.dao;

import android.util.Log;

import com.j256.ormlite.android.apptools.OpenHelperManager;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager;
import com.sk.weichat.MyApplication;
import com.sk.weichat.bean.User;
import com.sk.weichat.bean.redpacket.RedPacketNoticeBean;
import com.sk.weichat.db.SQLiteHelper;
import com.sk.weichat.ui.base.CoreManager;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lzs
 * @date 2019/10/9
 * Email:343067508@qq.com
 **/
public class RedPacketNoticeDao {
    private Dao<RedPacketNoticeBean, Integer> dao;

    private RedPacketNoticeDao() {
        try {
            dao = DaoManager.createDao(OpenHelperManager.getHelper(MyApplication.getInstance(), SQLiteHelper.class).getConnectionSource(), RedPacketNoticeBean.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static final RedPacketNoticeDao getInstance() {
        return SingletonHolder.instance;
    }

    static final class SingletonHolder {
        static RedPacketNoticeDao instance = new RedPacketNoticeDao();
    }

    public void insert(RedPacketNoticeBean redPacketNoticeBean) {
        try {
            String userID = CoreManager.getSelf(MyApplication.getInstance()).getUserId();
            redPacketNoticeBean.setToUserId(userID);
            dao.createOrUpdate(redPacketNoticeBean);
        } catch (SQLException e) {
            Log.e("lzs", "insert 红包消息 error:" + e.getSQLState());
            e.printStackTrace();
        }
    }

    public List<RedPacketNoticeBean> queryAll() {
        List<RedPacketNoticeBean> list = new ArrayList<>();
        try {
            String userID = CoreManager.getSelf(MyApplication.getInstance()).getUserId();
            List<RedPacketNoticeBean> newList = dao.queryBuilder().orderBy("timeSend", false).where().eq("toUserId", userID).query();
            if (newList != null) {
                list.addAll(newList);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 更新红包通知
     *
     * @param redPacketNoticeBean
     */
    public void update(RedPacketNoticeBean redPacketNoticeBean) {
        try {
            String userID = CoreManager.getSelf(MyApplication.getInstance()).getUserId();
            redPacketNoticeBean.setToUserId(userID);
            dao.update(redPacketNoticeBean);
        } catch (Exception e) {
            Log.e("lzs", "查询所有未读红包通知 Exception:" + e.getMessage());
        }
    }

    /**
     * @return 查询所有未读红包通知
     */
    public List<RedPacketNoticeBean> queryUnRead() {
        List<RedPacketNoticeBean> list = new ArrayList<>();
        try {
            String userID = CoreManager.getSelf(MyApplication.getInstance()).getUserId();
            List<RedPacketNoticeBean> listNew = dao.queryBuilder().orderBy("timeSend", false).where().eq("isRead", 0).and().eq("toUserId", userID).query();
            if (null != listNew) {
                list.addAll(listNew);
            }
        } catch (SQLException e) {
            Log.e("lzs", "查询所有未读红包通知 SQLException:" + e.getErrorCode());
            e.printStackTrace();
        }
        return list;
    }


}
